package com.mobisys.live.controller.statisticcontroller;


import com.mobisys.live.bean.video.GetVideoId;
import com.mobisys.live.dao.mapper.UserMapper;
import com.mobisys.live.dao.model.User;
import com.mobisys.live.dao.model.UserLiveWatchingRecord;
import com.mobisys.live.dao.model.UserVideoWatchingRecord;
import com.mobisys.live.response.Response;
import com.mobisys.live.response.statistic.UserLiveRecordResponseBody;
import com.mobisys.live.service.UserVideoWatchingRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

import static com.mobisys.live.config.ResponseCode.*;

/**
 * @author gwg
 * @date 2018/4/18 15:34
 */
@RestController
public class VideoStatisticController {
    @Autowired
    private UserVideoWatchingRecordService userVideoWatchingRecordService;

    @Autowired
    private UserMapper userMapper;
    @RequestMapping(value = "/statistic/userVideoStatistics",method = RequestMethod.POST)
    public Response userVideoStatistics(@RequestBody GetVideoId getVideoId){
        Response response = new Response();

        List<UserVideoWatchingRecord> userVideoWatchingRecordList = userVideoWatchingRecordService.selectByVideoId(getVideoId.getVideoId());
        if(userVideoWatchingRecordList.size()==0){
            response.setRetureValue(DATABASE_ERROR);
            response.setRetureMsg(MSG_DATABASE_ERROR);
            return response;
        }
        List<UserLiveRecordResponseBody> userVideoRecordResponseBodyList = new ArrayList<UserLiveRecordResponseBody>();
        for(UserVideoWatchingRecord userVideoWatchingRecord:userVideoWatchingRecordList){
            UserLiveRecordResponseBody userLiveRecordResponseBody = new UserLiveRecordResponseBody();
            userLiveRecordResponseBody.setUserId(userVideoWatchingRecord.getUserId());
            if(!userVideoWatchingRecord.getUserId().equals("anonymous_users")) {
                User user = userMapper.selectByPrimaryKey(userVideoWatchingRecord.getUserId());
                if (user==null){
                    response.setRetureValue(DATABASE_ERROR);
                    response.setRetureMsg(MSG_DATABASE_ERROR);
                    return response;
                }else {
                    userLiveRecordResponseBody.setUserName(user.getUsername());
                }
            }else {
                userLiveRecordResponseBody.setUserName("匿名用户");
            }
            userLiveRecordResponseBody.setUa(userVideoWatchingRecord.getUa());
            userLiveRecordResponseBody.setVisitIp(userVideoWatchingRecord.getVisitIp());
            userLiveRecordResponseBody.setVisitTime(userVideoWatchingRecord.getVisitTime());
            userLiveRecordResponseBody.setLeaveTime(userVideoWatchingRecord.getLeaveTime());
            userVideoRecordResponseBodyList.add(userLiveRecordResponseBody);
        }
        response.setRetureValue(SUCCESS);
        response.setRetureMsg(MSG_SUCCESS);
        response.setRetureData(userVideoRecordResponseBodyList);
        return response;
    }
}
